﻿using FZKJ.COMMON;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FZKJ.ZHXJ.Controllers
{
    public class DepartmentController : Controller
    {
        //
        // GET: /CarAgent/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 部门管理
        /// </summary>
        /// <returns></returns>
        public ActionResult DptManage()
        {
            return View();
        }

        /// <summary>
        /// 员工档案
        /// </summary>
        /// <returns></returns>
        public ActionResult EmployeeFlie()
        {
            return View();
        }
        public string GetEmployees()
        {
            var data = new ResultInfo<List<object>>();
            var pageSize = int.Parse(Request.Params["pagesize"]);
            var pageNo = int.Parse(Request.Params["pageno"]);
            var order = Request.Params["order"];
            //查询条件组织
            var where = " (isdel is null or isdel=0) AND ";
            if (!string.IsNullOrWhiteSpace(Request.Params["query"]))
            {
                where += $" name like'%{Request.Params["query"]}%' AND";
            }
            if (!string.IsNullOrWhiteSpace(Request.Params["parts"]))
            {
                where += $" depart={Request.Params["parts"]} AND ";
            }
            if (!string.IsNullOrEmpty(where))
                where = where.Substring(0, where.Length - 4);
            var startIndex = pageSize * (pageNo - 1) + 1;
            var endIndex = pageSize * pageNo;
            var dt = new BLL.buss_employee().GetListByPage(where, order, startIndex, endIndex).Tables[0];
            data.IsSucceed = true;
            data.TotalCount = new BLL.buss_employee().GetListByPage(where, order, -1, -1).Tables[0].Rows.Count;
            data.Entity = JsonHelper.DataTableToList(dt);
            return JsonConvert.SerializeObject(data);
        }
        [HttpPost]
        public string SaveEmployee(string json)
        {
            ResultInfo result = new ResultInfo(false);
            try
            {
                LoginInfo li = SessionHelper.ReadSession<LoginInfo>(SessionKeys.LoginInfoKey);
                var model = JsonConvert.DeserializeObject<Model.buss_employee>(json);
                if (model.id == -1)
                {
                    model.creater = new BLL.base_userinfo().GetModelList(" username='" + li.UserName + "'")[0].id;
                    model.createtime = DateTime.Now;
                    new BLL.buss_employee().Add(model);
                    result.IsSucceed = true;
                    result.Message = "添加成功";
                }
                else
                {
                    var m = new BLL.buss_employee().GetModel(model.id);
                    m.code = model.code;
                    m.name = model.name;
                    m.sex = model.sex;
                    m.depart = model.depart;
                    m.mobile = model.mobile;
                    new BLL.buss_employee().Update(m);
                    result.IsSucceed = true;
                    result.Message = "添加成功";

                }
            }
            catch (Exception exception)
            {
                result.IsSucceed = false;
                result.Message = "添加失败";
            }

            return JsonConvert.SerializeObject(result);
        }

        [HttpPost]
        public string DeleteEmployee(string ids)
        {
            ResultInfo result = new ResultInfo(false);
            try
            {
                foreach (var item in ids.Split(','))
                {
                    var model = new BLL.buss_employee().GetModel(Convert.ToInt32(item));
                    model.isdel = 1;
                    model.deltime = DateTime.Now;
                    new BLL.buss_employee().Update(model);
                }
                result.IsSucceed = true;
                result.Message = "删除成功";
            }
            catch (Exception e)
            {
                result.IsSucceed = false;
                result.Message = "删除失败";
            }
            return JsonConvert.SerializeObject(result);
        }
    }
}
